

namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_SelectInList_RT : RT_Node
    {
        public RT_ValueListPort ValueList_RT;

        public RT_SingleValuePort Index_RT;

        public RT_SingleValuePort Result_RT;

        public RT_SingleValuePort Valid_RT;

        public Node_SelectInList_RT(Node_SelectInList sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            ValueList_RT = new RT_ValueListPort(EPortIOType.Input, sourceData.ValueList, this);

            Index_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.Index, this);

            Result_RT = new RT_SingleValuePort(EPortIOType.Output, sourceData.Result, this);
            Result_RT.OnGetValue = GetResult;

            Valid_RT = new RT_SingleValuePort(EPortIOType.Output, sourceData.Valid, this);
            Valid_RT.OnGetValue = GetValid;

        }

        public AValue GetResult(DAG_Executor executor)
        {
            int index = Index_RT.GetValue(executor).ToInteger().Item2;
            var list = ValueList_RT.GetValueList(executor);
            if (index >= 0 && index < list.Count)
            {
                Result_RT.SourceValue.Assign(list[index]);
            }
            else
            {
                Result_RT.SourceValue.Reset();
            }
            return Result_RT.SourceValue;
        }

        public AValue GetValid(DAG_Executor executor)
        {
            int index = Index_RT.GetValue(executor).ToInteger().Item2;
            var list = ValueList_RT.GetValueList(executor);
            bool valid = index >= 0 && index < list.Count;
            Valid_RT.SourceValue.SetBoolean(valid);
            return Valid_RT.SourceValue;
        }


    }
}